//
//  SEGVisitorAccessCodeViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/6/4.
//

#import "SEGVisitorAccessCodeViewModel.h"
#import "SEGVisitorAccessDoorModel.h"
#import "SEGVisitorAccessManager.h"

@implementation SEGVisitorAccessCodeViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        self.codeModel = [SEGVisitorAccessManager shareManager].codeModel;
        self.shareUrl = [NSString stringWithFormat:@"%@/h5/passport/index.html?shareCode=%@&organId=%@&app_platform_code=%@&company_platform_code=%@", kClientRootURL, self.codeModel.shareCode, self.codeModel.organId, kAppPlatformCode, kAppPlatformCode];
    }
    return self;
}
/// 查询是否存在海康门
- (void)seg_searchHaiKangDoor {
    // 遍历是否存在海康门
    WeakSelf;
    [self.codeModel.visitorInitDoors enumerateObjectsUsingBlock:^(SEGVisitorAccessDoorModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.devicetype.integerValue == 8) {
            weakSelf.haikangDoorModel = obj;
            *stop = YES;
        }
    }];
    if (self.haikangDoorModel) {
        self.qrcodeDisplayStyle = SEGVisitorQRCodeDisplayLoadingStyle;
        // 请求海康二维码
        [self seg_requestHaiKangQRCode];
    } else {
        // 不存在海康门的时候，如果有普通二维码门，则展示二维码
        [self.codeModel.visitorDoors enumerateObjectsUsingBlock:^(SEGVisitorAccessDoorModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.doorType.integerValue == 3) {
                weakSelf.qrcodeWebString = obj.qrCodeUrl;
                weakSelf.qrcodeDisplayStyle = SEGVisitorQRCodeDisplayWebImageStyle;
                *stop = YES;
            }
        }];
        if (self.refreshBlock) self.refreshBlock(NO);
    }
}
/// 请求海康二维码
- (void)seg_requestHaiKangQRCode {
    if (self.refreshBlock) self.refreshBlock(YES);
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/door-restapi/v1/userapp/generateQRCodeForDoor",nil];
    NSDictionary *param = @{@"deviceType" : @([self.haikangDoorModel.devicetype integerValue]),
                            @"communityId" : kAccountCommunityID ?: @"",
                            @"tel" : kAccountMobile ?: @"",
                            @"deviceId" : self.haikangDoorModel.deviceid ?: @""
    };
    WeakSelf;
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:param finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        // codeType 0：成功；-1：失败；2：无门禁权限
        if ([codeType integerValue] == 2) {
            weakSelf.qrcodeDisplayStyle = SEGVisitorQRCodeDisplayNoAuthStyle;
            if (weakSelf.refreshBlock) weakSelf.refreshBlock(NO);
            return;
        }
        // 二维码不为空则展示
        NSString *QRCodeString = result[@"data"] ?: @"";
        if (isSuccessed && QRCodeString.length > 0) {
            weakSelf.qrcodeDisplayStyle = SEGVisitorQRCodeDisplayNormalStyle;
            weakSelf.qrcodeString = QRCodeString;
            if (weakSelf.refreshBlock) weakSelf.refreshBlock(NO);
            return;
        }
        // 其他情况都是加载失败
        weakSelf.qrcodeDisplayStyle = SEGVisitorQRCodeDisplayLoadFailStyle;
        if (weakSelf.refreshBlock) weakSelf.refreshBlock(NO);
    }];
}

/// 获取分享的信息
- (NSDictionary *)seg_getShareInfo {
    NSString *imageUrl = [NSString stringWithFormat:@"%@/picture/share/img_share_pass_g@2x.png", kClientImageRootURL];
    NSMutableDictionary *mParam = [@{} mutableCopy];
    
    
    mParam =  [@{@"shareImageUrlString" :imageUrl,
                 @"shareTypesArr"       : @[@(SEGShareVCActionType_Share_Message),
                                            @(SEGShareVCActionType_Share_WeChat),
                                            @(SEGShareVCActionType_Share_QQ)],
                 @"sharetitle"          :@"访客通行证",
                 @"sharecontent"        :@"请在进入园区时，出示此通行证给门岗。",
                 @"inviteurl"           :self.shareUrl ?: @"",
                 @"shareTypesArrType"           :@"2"
    } mutableCopy];
    [mParam setObject:@[@(SEGShareVCActionType_Share_WeChat)] forKey:@"shareTypesArr"];
    return mParam;
}

@end
